Page({
    data: {
      currentTab: 0,
      height: 0,
      loading1 : true,
      loading2: true,
      loading3: true,
      params: {
        option: -2,
        pageNum: 1,
        pageSize: 999999
      },
      list: [],
      list2: [],
      list3: []
    },

    onLoad () {
      // this.getHeight()
    },
    // 全部模块上拉加载
    scrollToLower (e) {
      console.log(1111111)
    },
    //查看附近的收书点
    nearShop (e) {
      var orderNo = e.currentTarget.dataset.orderno
      var shipType = e.currentTarget.dataset.shiptype
      var bookInfoIds = e.currentTarget.dataset.bookinfoids
      var cancelinfo = e.currentTarget.dataset.cancelinfo
      wx.navigateTo({
        url: '/package/juanshu/pages/nearShop/nearShop?orderNo=' + orderNo + '&shipType=' + shipType + '&fromType=2' + '&cancelInfo=' + cancelinfo + '&bookInfoIds=' + JSON.stringify(bookInfoIds) ,
      })
    },

    //获取屏幕高度

    getHeight () {
      var _this = this
      wx.getSystemInfo({
        success: function(res) {
          let clientHetght = res.windowHeight
          let clientWidth = res.windowWidth
          let ratio = 750 / clientWidth
          let height = clientHetght * ratio
          _this.setData({
            height: height
          })
        },
      })
    },

    //头部切换
    tabClick (e) {
      console.log(e.currentTarget.dataset.curr)
      var cur = e.currentTarget.dataset.curr
      var options = "params.option"
      this.setData({
        currentTab: cur,
        [options]: cur == 0 ? -2: cur == 1 ? "0,1,-1": 2
      })

      if (this.data.params.option == -2) {
        if (this.data.loading1 == false) {
          return false
        }else{
          this.getList(this.data.params)
        }
      }

      if (this.data.params.option == "0,1,-1") {
        if (this.data.loading2 == false) {
          return false
        }else{
          this.getList(this.data.params)
        }
      }

      if (this.data.params.option == 2) {
        if (this.data.loading3 == false) {
          return false
        } else {
          this.getList(this.data.params)
        }
      }

    },
    //切换
    switchTab (event) {
      var cur = event.detail.current;
      var options = "params.option"
      this.setData({
        currentTab: cur,
        [options]: cur == 0 ? -2 : cur == 1 ? "0,1,-1" : 2
      })

      if (this.data.params.option == -2) {
        if (this.data.loading1 == false) {
          return false
        } else {
          this.getList(this.data.params)
        }
      }

      if (this.data.params.option == "0,1,-1") {
        if (this.data.loading2 == false) {
          return false
        } else {
          this.getList(this.data.params)
        }
      }

      if (this.data.params.option == 2) {
        if (this.data.loading3 == false) {
          return false
        } else {
          this.getList(this.data.params)
        }
      }
    },


    //接口部分
    getList (params) {

      console.log(params.option)
      var _this = this
      wx.request({
        url: 'https://devapi.xiaoshitimebank.com/user/sdx/sdx/order/donate/list/auth',
        data: params,
        method: 'POST',
        header: {
          "content-type": "application/x-www-form-urlencoded; charset=UTF-8",
        "token" : wx.getStorageSync('token'),
          "uid" : wx.getStorageSync('uuid')
        },
        success: (res) => {
          if (res.data.code == 200) {

              if (params.option == -2) {

                res.data.data.resultList.forEach(function (item,index) {
                  for (var i = 0; i < item.bookInfos.length;i++) {
                    item.bookInfos[i].createTime = _this.timeStampTurnTime(item.bookInfos[i].createTime/1000)
                  }
                })
                var arr = res.data.data.resultList
                _this.setData({
                  list: arr
                })
                _this.setData({
                  loading1: false
                })
              }

            if (params.option == "0,1,-1") {
              res.data.data.resultList.forEach(function (item, index) {
                for (var i = 0; i < item.bookInfos.length; i++) {
                  item.bookInfos[i].createTime = _this.timeStampTurnTime(item.bookInfos[i].createTime / 1000)
                }
              })
                var arr = res.data.data.resultList
                _this.setData({
                  list2: arr
                })
                _this.setData({
                  loading2: false
                })
              }

            if (params.option == 2) {
              res.data.data.resultList.forEach(function (item, index) {
                for (var i = 0; i < item.bookInfos.length; i++) {
                  item.bookInfos[i].createTime = _this.timeStampTurnTime(item.bookInfos[i].createTime / 1000)
                }
              })
              var arr = res.data.data.resultList
              _this.setData({
                list3: arr
              })
              _this.setData({
                loading3: false
              })
            }
          }else{
            wx.showToast({
              title: res.data.msg,
              icon: 'none'
            })
          }
        }
      })
    },

timeStampTurnTime(timeStamp){
    if(timeStamp > 0){
  var date = new Date();
  date.setTime(timeStamp * 1000);
  var y = date.getFullYear();
  var m = date.getMonth() + 1;
  m = m < 10 ? ('0' + m) : m;
  var d = date.getDate();
  d = d < 10 ? ('0' + d) : d;
  var h = date.getHours();
  h = h < 10 ? ('0' + h) : h;
  var minute = date.getMinutes();
  var second = date.getSeconds();
  minute = minute < 10 ? ('0' + minute) : minute;
  second = second < 10 ? ('0' + second) : second;
  return y + '-' + m + '-' + d;
}else {
  return "";
}
},


    //漂流日记
    goPiaoliu (e) {

    var bookids = e.currentTarget.dataset.bookid

      // var bookinfoid = e.currentTarget.dataset.bookinfoid

      var pic = e.currentTarget.dataset.coverpic

      var name = e.currentTarget.dataset.name

      var author = e.currentTarget.dataset.author


      var price = e.currentTarget.dataset.price

      var maxshubi = e.currentTarget.dataset.maxshubi

      // var

    // var bookpic = e.

      wx.navigateTo({
        url: '/package/my/pages/piaoliuNot/piaoliuNot?bookid=' + bookids + '&pic=' + pic + '&name=' + name + '&author=' + author + '&price=' + price + '&maxshubi=' + maxshubi,
      })
    },


    // 邮寄带支付
    goPay (e) {

      var bookInfos = e.currentTarget.dataset.bookinfos

      var shipType = e.currentTarget.dataset.shiptype

      var bookInfoIds = e.currentTarget.dataset.bookids

      var orderno = e.currentTarget.dataset.orderno

      var shippingAddressId = e.currentTarget.dataset.shippingaddressid

      var arr = []

      var arr2 = []
      var str

      for (var i = 0; i < bookInfoIds.length; i++) {
        arr.push(bookInfoIds[i].bookInfoId)
      }

      if (arr.length == 1) {
        str = arr[0]
      } else {
        str = arr.join(',')
      }

      for (var i = 0; i < bookInfos.length; i++) {
        arr2.push({
          coverPic: bookInfos[i].coverPic,
          name: bookInfos[i].name,
          price: bookInfos[i].price,
          maximumDiscount: bookInfos[i].maximumDiscount
        })
      }


      wx.navigateTo({
        url: '/package/my/pages/payDictionary/payDictionary?bookInfos=' + JSON.stringify(arr2) + '&orderNo=' + orderno + '&shiptype=' + shipType + '&shippingAddressId=' + shippingAddressId + '&bookInfoIds=' + str,

      })

      return

      var arr = []

      var str
      var bookInfoIds = e.currentTarget.dataset.bookids

      var shipType = e.currentTarget.dataset.shiptype

      var orderno = e.currentTarget.dataset.orderNo

      for (var i = 0;i<bookInfoIds.length;i++) {
        arr.push(bookInfoIds[i].bookInfoId)
      }

    if (arr.length == 1) {
      str = arr[0]
    }else{
      str = arr.join(',')
    }
      wx.navigateTo({
        url: '/package/juanshu/pages/addYouji/addYouji?bookInfoIds=' + str + '&shipType=' + shipType + '&orderNo=' + orderno,
      })
    },

    goPay2 () {
      return false
    },


  //取消订单
  cutOrder (orderNo) {

        for (var i = 0; i < this.data.list.length; i++) {
          if (orderNo == this.data.list[i].orderNo) {
            var list = this.data.list
            list.splice(i, 1)
            this.setData({
              list: list
            })
          }
        }



      for (var i = 0; i < this.data.list2.length; i++) {
        if (orderNo == this.data.list2[i].orderNo) {
          var list = this.data.list2
          list.splice(i, 1)
          this.setData({
            list2: list
          })
        }
      }


      for (var i = 0; i < this.data.list3.length; i++) {
        if (orderNo == this.data.list3[i].orderNo) {
          var list = this.data.list3
          list.splice(i, 1)
          this.setData({
            list3: list
          })
        }
      }

  },

    onShow () {
      this.getHeight()
      this.getList(this.data.params)
    }
})
